package com.nkzy.mapper;

import com.nkzy.pojo.User;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface UserMapper {

    // 查询所有用户
    @Select("SELECT * FROM user")
    List<User> selectAllUsers();

    // 根据ID查询用户
    @Select("SELECT * FROM user WHERE id = #{id}")
    User selectUserById(Long id);

    // 新增用户
    @Insert("INSERT INTO user (username, password, nickname, email, user_pic, create_time, update_time) " +
            "VALUES (#{username}, #{password}, #{nickname}, #{email}, #{userPic}, #{createTime}, #{updateTime})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insertUser(User user);

    // 更新用户
    @Update("UPDATE user SET username = #{username}, password = #{password}, nickname = #{nickname}, " +
            "email = #{email}, user_pic = #{userPic}, update_time = #{updateTime} WHERE id = #{id}")
    int updateUser(User user);

    // 删除用户
    @Delete("DELETE FROM user WHERE id = #{id}")
    int deleteUser(Long id);

    // 根据多个ID查询用户
    @Select("<script>" +
            "SELECT * FROM user WHERE id IN " +
            "<foreach item='id' index='index' collection='list' open='(' separator=',' close=')'>" +
            "#{id}" +
            "</foreach>" +
            "</script>")
    List<User> selectUsersByIds(List<Long> ids);


}